package dp.mobile.level;

import org.w3c.dom.Node;

import android.graphics.Point;

public class CampaignSelectLevelItem {
	public CampaignSelectLevelItem(Node root) {
		int childCount = root.getChildNodes().getLength();
		
		String pos = null;
		
		for (int i = 0; i < childCount; ++i) {
			Node child = root.getChildNodes().item(i);
			
			if (child.getNodeType() == Node.TEXT_NODE) continue;
			
			String tag = child.getNodeName();
			String val = child.getFirstChild().getNodeValue();
			
			if (tag.equals("source")) 			mLevelSource 	= val;
			else if (tag.equals("name"))		mName			= val;
			else if (tag.equals("position"))	pos				= val;
			else if (tag.equals("description"))	mDescription	= val;
			
		}
		
		String[] splitted = pos.split(",");
		for (int j = 0; j < splitted.length; ++j)
			splitted[j] = splitted[j].trim();
		
		// format description

		StringBuilder sb = new StringBuilder();
		int spaceCount = 0;
		for (int i = 0; i < mDescription.length(); ++i) {
			char c = mDescription.charAt(i);
			if (c == '\n' || c == '\t') {
				++spaceCount;
				if (spaceCount <= 1)
					sb.append(' ');
			} else if (c == ' ') {
				++spaceCount;
				if (spaceCount <= 1)
					sb.append(' ');
			} else {
				spaceCount = 0;
				sb.append(c);
			}
		}
		
		mDescription = sb.toString();
		
		mPosition = new Point(Integer.parseInt(splitted[0]), Integer.parseInt(splitted[1]));
	}
	
	public Point getPosition() {
		return mPosition;
	}
	
	public String getLevelSource() {
		return mLevelSource;
	}
	
	public String getName() {
		return mName;
	}
	
	public String getDescription() {
		return mDescription;
	}
	
	private Point mPosition;
	private String mLevelSource;
	private String mName;
	private String mDescription;
}
